package net.llz.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.llz.enums.BizCodeEnum;
import net.llz.interceptor.LoginInterceptor;
import net.llz.model.AddressDO;
import net.llz.model.LoginUser;
import net.llz.request.AddressAddReqeust;
import net.llz.service.AddressService;
import net.llz.util.JsonData;
import net.llz.vo.AddressVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 电商-公司收发货地址表 前端控制器
 * </p>
 *
 * @author Let-凡
 * @since 2023-01-03
 */

@Api(tags = "收货地址模块")
@RestController
@RequestMapping("/api/address/v1")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @ApiOperation("根据id查找地址")
    @GetMapping("find/{address_id}")
    public JsonData detail(
            @ApiParam(value = "地址id",required = true)
            @PathVariable("address_id") Long id){
        //int i =1/0;


        AddressVO addressVO = addressService.detail(id);

        return addressVO == null ? JsonData.buildResult(BizCodeEnum.ADDRESS_NO_EXITS):JsonData.buildSuccess(addressVO);

    }

    @ApiOperation("新增收获地址")
    @PostMapping("add")
    public JsonData add(
            @ApiParam("地址对象")
            @RequestBody AddressAddReqeust addressAddReqeust){

        addressService.add(addressAddReqeust);
        return JsonData.buildSuccess();

    }

    @DeleteMapping("del/{address_id}")
    @ApiOperation("删除地址")
    public JsonData del(
            @ApiParam(value = "地址id",required = true)
            @PathVariable("address_id") Integer address_id){

        Integer rows = addressService.del(address_id);

        return rows == null ? JsonData.buildResult(BizCodeEnum.ADDRESS_DEL_FAIL):JsonData.buildSuccess("删除成功");
    }

    @ApiOperation("查看全部地址")
    @GetMapping("list")
    public JsonData list(){



        List<AddressVO> list =  addressService.listUserAllAddress();

        return JsonData.buildSuccess(list);
    }

}

